/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.chw.calculator;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 *
 * @author JSJSYS
 */
public class SQLConnect {
    static final String DRIVER_CLASS = "org.h2.Driver";
   
   // static final String JDBC_URL = "jdbc:h2:C:/Users/JSJSYS";
    static final String JDBC_URL = "jdbc:h2:tcp://localhost/C:/Users/JSJSYS";
     // static final String JDBC_URL = "jdbc:h2:tcp://localhost/mem:mydb";
    static final String USER = "sa";

    static final String PASSWORD = "a950809";
     
   /* public static void main(String[] args) throws Exception {
        // create server
        
        // initServer();
        
    jdbcDemo();
 
         // ++++++++++++++++++++++++++++++++ormlite
         
    ormDemo();
    }
    /*public static void jdbcDemo() throws Exception{
        // 鍔犺浇椹卞姩
        Class.forName(DRIVER_CLASS);
        // 鏍规嵁杩炴帴URL锛岀敤鎴峰悕锛屽瘑鐮佽幏鍙栨暟鎹簱杩炴帴
        Connection conn = DriverManager.getConnection(JDBC_URL, USER, PASSWORD);
        // 鍒涘缓涓�涓猄tatement 
        Statement stmt = conn.createStatement();
        //濡傛灉瀛樺湪琛ㄥ氨鍏堝垹闄�
         stmt.execute("DROP TABLE IF EXISTS stu_info");
        //鍒涘缓stu_info琛�
         stmt.execute("CREATE TABLE stu_info(id VARCHAR(36) PRIMARY KEY,name VARCHAR(100),sex VARCHAR(4),student_id VARCHAR(50))");
        //鏂板4 鏉�
        stmt.executeUpdate("INSERT INTO stu_info VALUES('" + getId() + "','绋嬪噷闇�','鐢�','20151101066')");
        stmt.executeUpdate("INSERT INTO stu_info VALUES('" + getId() + "','闄堢浜�','濂�','20151101026')");
        stmt.executeUpdate("INSERT INTO stu_info VALUES('" + getId() + "','榛嶈矾','濂�','20151101056')");
        stmt.executeUpdate("INSERT INTO stu_info VALUES('" + getId() + "','鐢版ⅵ','濂�','20151101096')");
       
        p("++++++++++++++++++++++++++++++++++++++鏄剧ず鍒氫繚瀛樼殑鍥涙潯鏁版嵁锛�");
        show(stmt);
        p("");
        p(""); 
        p("");   
        //鍒犻櫎 闄堢浜�
        stmt.executeUpdate("DELETE FROM stu_info WHERE name='闄堢浜�'");
        p("++++++++++++++++++++++++++++++++++++++鍒犻櫎 闄堢浜� 鍚庯紝鏄剧ず缁撴灉锛�");
        show(stmt);
        
        p("");
        p("");
        
        //淇敼 榛嶈矾 === >榛庤矾
        stmt.executeUpdate("UPDATE stu_info SET name='榛庤矾' WHERE name='榛嶈矾'");
        p("=+++++++++++++++++++++++++++++++++淇敼 榛嶈矾 === >榛庤矾,鏄剧ず缁撴灉锛�");
        show(stmt);
      
        
        
        //閲婃斁stmt
        stmt.close();
        
        
        //鍏抽棴杩炴帴
        conn.close();
        System.out.println("------jdbc -end!!!------");

    }
    
     public static void ormDemo(){
        p("");
        p("");
        p("");
        p("");
        p("++++++++++++++++++++++++浠ヤ笅鏄痮rmlite 鎿嶄綔H2 ++++++++++++++++++++++++");
        StudentDAO studentDAOUtil =new StudentDAO(s);
        StudentService service = new StudentService(studentDAOUtil.getStudentDAO());
        //鏄剧ず鐩墠瀛︾敓琛ㄧ殑鎵�鏈夌殑璁板綍,骞跺彇寰楃涓�鏉℃暟鎹�
        StudentInfo first = showAll(service).get(0);
        p("");
        p("");
        p("===========================鏇存柊绗竴鏉℃暟鎹細濮撳悕 "+first.getName()+" 鏀规垚 鍒樺痉鍗�");
        first.setName("鍒樺痉鍗�");
        // update
        service.update(first);
        // show all
        showAll(service);
        p("");
        p("");
        p("============================鍒犻櫎绗竴鏉℃暟鎹細"+first.getName());
        // 鍒犻櫎绗竴鏉�
        service.del(first);
        // show all
        showAll(service);

        p("");
        p("");
        p("============================澧炲姞2鏉℃暟鎹細" );
        StudentInfo one = new StudentInfo();
        one.setId(7);
        one.setName("寮犲鍙�");
        one.setSex("鐢�");
        one.setStudentId("199210106666");
        service.save(one);// save
        
        StudentInfo two = new StudentInfo();
        two.setId(8);
        two.setName("鏅ф湀鏄�");
        two.setSex("濂�");
        two.setStudentId("199210108888");
        service.save(two);// save
        showAll(service);
        
        studentDAOUtil.close();
        
    }
    private static void show( Statement stmt) throws Exception{
        //鏌ヨ
        ResultSet rs = stmt.executeQuery("SELECT * FROM stu_info");
          while (rs.next()) {
            p(rs.getString(1) + "\t" + rs.getString("name") + "\t" + rs.getString("sex")+"\t"+rs.getString(4));
        }
    }
    static void p(String a){
       System.out.println(a);
    }
    private static int num =1;
    private static int getId(){
        return num++  ;
    }*/
}
